package com.wantupai.translate.widget.popuwindow;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.wantupai.translate.R;
import com.wantupai.translate.utils.CommonUtils;
import com.wantupai.translate.utils.ToastUtils;
import com.wantupai.translate.utils.VerifyUtil;

/**
 * @author luxuchang
 * @date 2019/5/7 0007
 * @description
 */
public class LoginPopuWindow extends PopupWindow implements View.OnClickListener {

    private Activity activity;
    private OnBtnClickListener listener;
    private EditText etPhone, etCode;
    private ImageView btnSendCode, btnLogin;
    private TextView tvXieyi, tvZhengce, tvSecond;

    public LoginPopuWindow(Activity activity, OnBtnClickListener listener) {
        this.activity = activity;
        this.listener = listener;
        View view = View.inflate(activity.getApplicationContext(), R.layout.pop_layout_login, null);
        initView(view);
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(false);
        setFocusable(true);
        setContentView(view);
    }

    private void initView(View view) {
        etPhone = view.findViewById(R.id.phone);
        etCode = view.findViewById(R.id.code);
        tvSecond = view.findViewById(R.id.tv_second);
        btnSendCode = view.findViewById(R.id.iv_send_code);
        btnLogin = view.findViewById(R.id.iv_login);
        tvXieyi = view.findViewById(R.id.tv_xieyi);
        tvZhengce = view.findViewById(R.id.tv_zhengce);

        btnSendCode.setOnClickListener(this);
        btnLogin.setOnClickListener(this);
        tvXieyi.setOnClickListener(this);
        tvZhengce.setOnClickListener(this);
    }

    public void startTimer() {
        btnSendCode.setVisibility(View.GONE);
        CommonUtils.startCountDown(tvSecond, R.color.color_333, R.color.color_333, 60000, "");
    }

    public void showCodeImage() {
        tvSecond.setVisibility(View.GONE);
        btnSendCode.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_send_code:
                String phoneNum = etPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNum)) {
                    ToastUtils.show("请输入手机号");
                    return;
                }
                if (!VerifyUtil.isMobileNO(phoneNum)) {
                    ToastUtils.show("请输入正确的手机号");
                    return;
                }
                if (listener != null) listener.OnBtnClick(0, phoneNum, "");
                break;
            case R.id.iv_login:
                String phoneNum1 = etPhone.getText().toString().trim();
                String code = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNum1)) {
                    ToastUtils.show("请输入手机号");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    ToastUtils.show("请输入验证码");
                    return;
                }
                if (listener != null) listener.OnBtnClick(1, phoneNum1, code);
                break;
            case R.id.tv_xieyi:
                if (listener != null) listener.OnBtnClick(2, "", "");
                break;
            case R.id.tv_zhengce:
                if (listener != null) listener.OnBtnClick(3, "", "");
                break;
        }
    }

    public void show() {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 0.3f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);
        showAtLocation(activity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }


    @Override
    public void dismiss() {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 1.0f;
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        activity.getWindow().setAttributes(lp);
        super.dismiss();
    }

    public interface OnBtnClickListener {
        void OnBtnClick(int type, String phoneNum, String codeNum);
    }
}
